﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightApplication1.MangerEmployes;
using System.Net.NetworkInformation;
using System.ServiceModel;
using System.Windows.Browser;

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            NetworkChange.NetworkAddressChanged += NetwordChanged;
            CheckNetworkState();
            btnShowData_Click();
        }
        private void NetwordChanged(object sender, EventArgs e)
        {
            CheckNetworkState();
        }
        private void CheckNetworkState()
        {
            //if (NetworkInterface.GetIsNetworkAvailable())
            //{
            //    btnShowData.IsEnabled = true;

            //}
            //else
            //    btnShowData.IsEnabled = false;

            //btnShowData.IsEnabled = true;
        }

        private void btnShowData_Click()
        {
            EndpointAddress address = new EndpointAddress("http://localhost:" +
                HtmlPage.Document.DocumentUri.Port + "/MangerAgency.svc");

            MangerAgencyClient proxy = new MangerAgencyClient();
            proxy.Endpoint.Address = address;
            
            proxy.GetDateTimeCompleted += new EventHandler<GetDateTimeCompletedEventArgs>(GetDateTimeCompleted);

            proxy.GetDateTimeAsync();

            proxy.GetAllDaiLyCompleted += new EventHandler<GetAllDaiLyCompletedEventArgs>(proxy_GetAllDaiLyCompleted);

            proxy.GetAllDaiLyAsync();    

        }

        void proxy_GetAllDaiLyCompleted(object sender, GetAllDaiLyCompletedEventArgs e)
        {
            radgvDaiLy.ItemsSource = e.Result;
        }
        private void GetDateTimeCompleted(object sender,GetDateTimeCompletedEventArgs e)
        {
            try
            {
                lblTime.Content = e.Result.ToLongTimeString();
            }
            catch (Exception err)
            {
                lblTime.Content = err.Message;
                if (err.InnerException != null) lblTime.Content += "\n" + err.InnerException.Message;
            }

        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            btnShowData_Click();
        }

        private void radgvDaiLy_DataLoaded(object sender, EventArgs e)
        {
           
        }

        private void radgvDaiLy_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {

        }
        


    }
}
